package com.molichuxing.baselibrary.utils;

import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 * log日志管理工具
 */
public class LogUtils {
    private static boolean ISLogCat = true;
    public static void i(String tag, String text){
        if(ISLogCat){
            Log.i(tag,text);
        }
    }

    public static void e(String tag, String text){
        if(ISLogCat){
            Log.e(tag,text);
        }
    }

    public static void ic(String tag, String text){
        if(ISLogCat){
            Log.i(tag,text);
            LogUtils.init().dumpEx2SdCard(tag+"-"+text,"catchlog");
        }
    }

    public static boolean ISLogCat() {
        return ISLogCat;
    }

    public static void setISLogCat(boolean ISLogCat) {
        LogUtils.ISLogCat = ISLogCat;
    }

    private static LogUtils logUtils;
    public static LogUtils init(){
        if(logUtils==null){
            logUtils = new LogUtils();
        }
        return logUtils;
    }

    public static abstract class LogExCallback {
        public abstract void onFailed();
        public abstract void onSuccess();
    }

    private LogExCallback logExCallback;
    public void dumpEx2SdCard(final String loginfo, final String fileName, LogExCallback mlogExCallback){
        this.logExCallback = mlogExCallback;
        dumpEx2SdCard(loginfo,fileName);
    }

    public void dumpEx2SdCard(final String loginfo, final String fileName)
    {
//        Log.i("text","文件写入开始");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    File filePerent = new File(ImageTools.getSDPath()+"/moli/molilog");
                    if(!filePerent.exists()){
                        filePerent.mkdirs();
                    }
                    File file = new File(ImageTools.getSDPath()+"/moli/molilog/"+fileName+".log");
                    long cacheSize = 180*1000;
                    if(getFileSize(file)>cacheSize) {
                        file.delete();
                    }
//                    Log.i("text","文件写入中------");
                    String logPath = ImageTools.getSDPath()+"/moli/molilog";
                    FileWriter fw = new FileWriter(logPath + File.separator
                            + fileName+".log", true);
                    fw.write(TimeUtils.getDateTime()+"\n");
                    fw.write(loginfo+"\n");
                    fw.write("\n");
                    fw.close();

//                    PrintWriter printWriter = null;
//                    printWriter = new PrintWriter(new BufferedWriter(new FileWriter(file)));
//                    printWriter.print("\n"+TimeUtils.getTime()+"\n");
//                    printWriter.print(loginfo+"\n");
                    Log.i("text","文件写入成功");
                    if(logExCallback!=null){
                        logExCallback.onSuccess();
                    }
                } catch (Exception e) {
                    Log.i("text","文件写入失败");
                    if(logExCallback!=null){
                        logExCallback.onFailed();
                    }
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 获取指定文件的大小
     *
     * @return
     * @throws Exception
     */
    public static long getFileSize(File file) {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(file);//使用FileInputStream读入file的数据流
                size = fis.available();//文件的大小
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return size;
    }

}
